// pages/completed/completed.js
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    url: '',
    record: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      url: getApp().globalData.url,
      userInfo: getApp().globalData.yukaiUser
    });
    this.getRecord(0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getRecord: function (type) {
    wx.showLoading({});
    var userInfo = this.data.userInfo ? this.data.userInfo : getApp().getUser();
    var url = this.data.url ? this.data.url : getApp().getUrl();
    var _self = this;
    wx.request({
      url: url + '/repairRecord/showCompleteRepairRecord',
      data: {
        token: userInfo.token,
        staffId: userInfo.staffId
      },
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data);
        var data = res.data;
        switch (data.code) {
          case 200:
            _self.setData({
              record: _self.convert(data.obj)
            });
            wx.hideLoading();
            break;
          case 412:
            wx.hideLoading();
            utils.alertBox(data.msg);
            break;
          case 422:
            wx.hideLoading();
            utils.toLogin();
            break;
          default:
            wx.hideLoading();
            break;
        }
      },
      fail: function () {
        wx.hideLoading();
        utils.alertBox('请求失败，请检查网络');
      }
    });
  },
  check: function (e) {
    wx.navigateTo({
      url: '../repairdetail/repairdetail?repairRecordId=' + e.currentTarget.dataset.machine.repairRecordId,
    })
  },
  convert: function (items) {
    var newItems = [];
    for (var i = 0; i < items.length; i++) {
      newItems.push({
        repairRecordId: items[i].repairRecordId,
        equipmentName: items[i].equipmentName,
        equipmentSerial: items[i].equipmentSerial,
        hospitalName: items[i].hospitalName,
        departmentName: items[i].departmentName,
        repairDate: utils.formatTime(new Date(items[i].repairDate)),
        staffName: items[i].staffName,
        acceptanceStatus: items[i].acceptanceStatus,
        eqiupmentModel: items[i].eqiupmentModel,
        repairStatus: items[i].repairStatus
      });
    }
    return newItems;
  }
})